<%@ page contentType="text/html; charset=utf-8" %>

<%@ include file="/WEB-INF/jsp/includeTaglib.jsp" %>
<div id="ajaxpagetoolbar">
		<input type="hidden" id="insertFunctionKey" name="insertFunctionKey">
    <table width="100%" border="0" cellpadding="0" cellspacing="1">
      <tr class="rowHead">
        <td height="20" width="20" align="center"><input type="checkbox" id="checkAll" onclick="javascript:checkAllOrNot();"></td>
        <td height="20" nowrap align="center" class="globalTextBold">函数类型</td>
        <td height="20" nowrap align="center" class="globalTextBold">函数名称</td>
        <td height="20" nowrap align="center" class="globalTextBold">函数描述</td>
        <td height="20" nowrap align="center" class="globalTextBold">对应表</td>
        <td height="20" nowrap align="center" class="globalTextBold">对应字段</td>
        <td height="20" nowrap align="center" class="globalTextBold">关联字段</td>
      </tr>
      <c:if test="${empty functionTemplates}">
       <tr class="rowEven">
        <td colspan="7" align="center" class="globalText">无数据</td>
       </tr>
			</c:if>
      <c:forEach var="functionTemplate" items="${functionTemplates}" varStatus="status">
       <c:choose>
        <c:when test="${status.index % 2 == 0}">
        <tr class="rowEven">
        </c:when>
        <c:otherwise>
        <tr class="rowOdd">
        </c:otherwise>
       </c:choose>
         <td height="20" width="20" align="center"><input type="checkbox" name="cbIds" value="${functionTemplate.id}" onclick="javascript:checkPerBox();"></td>
         <td height="20" class="globalText">${functionTemplate.functionType == "0" ? "数据函数" : "系统函数"}</td>
         <td height="20" class="globalText">${functionTemplate.functionName}</td>
         <td height="20" class="globalText">${functionTemplate.description}</td>
         <td height="20" class="globalText">
           <c:if test="${functionTemplate.relationTable == 'dorder'}">运单表</c:if>
           <c:if test="${functionTemplate.relationTable == 'bdepartment'}">组织结构表</c:if>
           <c:if test="${functionTemplate.relationTable == 'dclient'}">客户表</c:if>
         </td>
         <td height="20" class="globalText">${functionTemplate.relationField}</td>
         <td height="20" class="globalText">${functionTemplate.associatedField}</td>
       </tr>
      </c:forEach>
       <tr>
         <td align="left" colspan="2">
           <table border="0" cellpadding="0" cellspacing="2">
             <tr>
               <td><input id="insertFunctionBtn" type="button" class="btnStyle" value="  插入"></td>
             </tr>
           </table>
         </td>
       </tr>
    </table>

<%@ include file="/WEB-INF/jsp/functiontemplate/quickViewFunctionPagedDisplay.jsp" %>
</div>


<script type="text/javascript">

	function checkInsert() {
		if(document.getElementsByName("cbIds") == null){
			return false;
		}

		if(!checkAtLeastOne()){
			alert("请至少选择一条记录");
			return false;
		}

		if(!checkOnlyOne()){
			alert("请只选择一条记录");
			return false;
		}

		if (document.getElementsByName("cbIds").length == 1){
			document.getElementById("insertFunctionKey").value = document.getElementsByName("cbIds")[0].value;
		}else{
			for(var i=0;i<document.getElementsByName("cbIds").length;i++){
				if(document.getElementsByName("cbIds")[i].checked){
					document.getElementById("insertFunctionKey").value = document.getElementsByName("cbIds")[i].value;
					break;
				}
			}
		}
		return true;

		//执行插入
		//document.getElementById("newMsgTemplateForm").action = "<c:url value='msgTemplateMain.html' />";
		//document.getElementById("newMsgTemplateForm").submit();
	}

	$(document).ready(function() {
  	var type = ${type};

	  $("#insertFunctionBtn").click(function(){
		  if(checkInsert()) {
			  var insertFunctionKey = $("#insertFunctionKey").val();
      	$.get("<c:url value='getInsertFunctionByKey.html' />", {insertFunctionKey:insertFunctionKey},
	      	  function(data){
	      	    if (type == "0") {
								$("#smsContent").insertAtCaret(data);
		      	  } else {
								$("#voiceContent").insertAtCaret(data);
			      	}
	        });
			}
	  });
	});

</script>